// 抽象 修改
export function baseUpdate(this_, path, parameter, initTable = () => {}) {
  return new Promise(resolve => {
    this_
      .$$post(path, parameter)
      .then(resp => {
        if (resp && resp.success === true) {
          this_.$message({
            type: 'success',
            message: resp.message
          })
          initTable()
          resolve(true)
        } else {
          this_.$message({
            type: 'warning',
            message: resp.message
          })
          resolve(false)
        }
      })
      .catch(() => {
        this_.disabledBtn = false
      })
  })
}

// 抽象 保存
export function baseSave(this_, path, parameter) {
  return new Promise(resolve => {
    this_
      .$$post(path, parameter)
      .then(resp => {
        if (resp && resp.code == '200') {
          this_.$message.success('保存成功')
          resolve(true)
        } else {
          this_.$message.error('保存失败')
          resolve(false)
        }
      })
      .catch(() => {})
  })
}

// 抽象 批量删除
export function baseBatchDelete(this_, path, ids, init = () => {}) {
  return new Promise(resolve => {
    this_.$confirm({
      title: '确定是否删除数据？',
      okText: '是',
      okType: 'danger',
      cancelText: '否',
      onOk() {
        this_.$$post(path, ids).then(resp => {
          if (resp && resp.code === 200) {
            this_.$message.success('删除成功')
            init()
            resolve(true)
          } else {
            this_.$message.error('删除失败')
            resolve(false)
          }
        })
      },
      onCancel() {
        // console.log('Cancel')
      }
    })
  })
}
// 抽象 删除 get
export function baseRemove(this_, path, initTable = () => {}) {
  return new Promise(resolve => {
    this_
      .$confirm('此操作将永久删除对应的记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      .then(() => {
        this_.$$get(path).then(resp => {
          if (resp && resp.success === true) {
            this_.$message({
              type: 'success',
              message: resp.message
            })
            initTable()
            resolve(true)
          } else {
            this_.$message({
              type: 'warning',
              message: resp.message
            })
            resolve(false)
          }
        })
      })
      .catch(() => {
        this_.$message({
          type: 'info',
          message: '已取消操作'
        })
      })
  })
}
// 抽象 删除 post
export function baseRemoved(this_, path, parameter, initTable = () => {}) {
  return new Promise(resolve => {
    this_
      .$confirm('此操作将永久删除对应的记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      .then(() => {
        this_.$$post(path, parameter).then(resp => {
          if (resp && resp.success === true) {
            this_.$message({
              type: 'success',
              message: resp.message
            })
            initTable()
            resolve(true)
          } else {
            this_.$message({
              type: 'warning',
              message: resp.message
            })
            resolve(false)
          }
        })
      })
      .catch(() => {
        this_.$message({
          type: 'info',
          message: '已取消操作'
        })
      })
  })
}
// 抽象 列表数据加载
export function updateTabel(this_, path, parameter) {
  return new Promise(resolve => {
    this_.$$post(path, parameter).then(resp => {
      if (resp && resp.code === 200) {
        resolve(resp)
      } else {
        this_.$message.error('加载失败')
        resolve(resp.message)
      }
    })
  })
}
// 获取当前时间
export function getNowTime() {
  let date = new Date()
  let month = date.getMonth() + 1
  let strDate = date.getDate()
  if (month >= 1 && month <= 9) {
    month = '0' + month
  }
  if (strDate >= 0 && strDate <= 9) {
    strDate = '0' + strDate
  }
  let currentDate =
    date.getFullYear() +
    '-' +
    month +
    '-' +
    strDate +
    ' ' +
    date.getHours() +
    ':' +
    date.getMinutes() +
    ':' +
    date.getSeconds()

  return currentDate
}
